package com.wcy.bysj.controller;

import com.wcy.bysj.common.MyException;
import com.wcy.bysj.common.Result;
import com.wcy.bysj.common.ResultEnum;
import com.wcy.bysj.entity.Food;
import com.wcy.bysj.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/food")
public class FoodController {

    @Autowired
    private FoodService foodService;

    @RequestMapping("/addFood")
    @ResponseBody
    public Result addFood(@RequestBody Food food) {
        try{
            return foodService.addFood(food);
        } catch (MyException me) {
            me.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"新增失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"新增失败");
        }
    }

    @RequestMapping("/selectAddFood")
    @ResponseBody
    public Result selectAllFood(@RequestBody Food food) {
        try{
            return foodService.selectAllFood(food);
        } catch (MyException me) {
            me.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"新增失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"新增失败");
        }
    }

    @RequestMapping("/updateFood")
    @ResponseBody
    public Result updateFood(@RequestBody Food food) {
        try{
            return foodService.updateFood(food);
        } catch (MyException me) {
            me.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"新增失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"新增失败");
        }
    }

    @RequestMapping("/deleteFood")
    @ResponseBody
    public Result deleteFood(@RequestBody Food food) {
        try{
            return foodService.deleteFood(food);
        } catch (MyException me) {
            me.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"新增失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(ResultEnum.ERROR.getCode(),"新增失败");
        }
    }

}
